from tabnanny import verbose
from django.db import models
from django.urls import reverse
from product.models import Product
from thinghoo_library.models import TimeModel


# Create your models here.
class ReleaseInfo(TimeModel):
    # 发行基本信息
    product = models.ForeignKey(Product, name='product', verbose_name='产品名称', on_delete=models.CASCADE)
    release_version = models.CharField(verbose_name="发行版本", max_length=16, unique=True)

    submit_time = models.DateTimeField(verbose_name='提审时间', null=True)
    publish_time = models.DateTimeField(verbose_name="发行时间", null=True)

    deleted_at = models.DateTimeField(verbose_name='删除时间', null=True)
    release_remarks = models.CharField(verbose_name="发行备注", max_length=64)
    # 添加或修改功能
    update_front = models.TextField(verbose_name="前端添加或更改功能", max_length=1024)
    update_back = models.TextField(verbose_name="后端添加或更改功能", max_length=1024)
    # 修复的 BUG
    fixed_front = models.TextField(verbose_name="前端修复的BUG", max_length=1024)
    fixed_back = models.TextField(verbose_name="后端修复的BUG", max_length=1024)
    # 存在的问题
    problems = models.TextField(verbose_name="存在的问题", max_length=1024)
    # Commit Hash
    hash_front = models.CharField(verbose_name="前端提交Hash", max_length=64)
    hash_back = models.CharField(verbose_name="后端提交的Hash", max_length=64)
    # 表单状态
    status_choices = (
        (1, "未提交"),
        (2, "未审核"),  # 已提交但是没有审核
        (3, "已拒绝"),
        (4, "审核通过")
    )

    reject_reason = models.TextField()

    status = models.SmallIntegerField(
        verbose_name="草稿状态",
        choices=status_choices,
        default=1
    )

    task_id = models.CharField(max_length=100, null=True)

    def get_absolute_url(self):
        return reverse('draft_detail', args=(self.id,))

    def __str__(self) -> str:
        return f'<ReleaseInfo {self.id}>'


class BuildRecord(TimeModel):
    release_info = models.ForeignKey(ReleaseInfo, on_delete=models.DO_NOTHING)
    version = models.CharField(max_length=10)

    task_id = models.CharField(max_length=20, null=True)

    status_choices = (
        (1, "未开始"),
        (2, "构建中"),  # 已提交但是没有审核
        (3, "构建成功"),
        (4, "构建失败")
    )
    status = models.SmallIntegerField(
        verbose_name="构建状态",
        choices=status_choices,
        default=1
    )

    def __str__(self) -> str:
        return f'<Build {self.id}, version: {self.version}>'
